<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		
	<span class="different-posts different-posts_earlier">📖 <a href="/page/11">earlier posts</a> 📖</span>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/22. shell 练习-找出活动ip/">shell 练习-找出活动ip</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>写一个脚本，实现判断192.168.0.0网络里，当前在线的IP有哪些</p>
<p>思路： for循环， 0.1 –  0.254  依次去ping，能通说明在线。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">ips=<span class="string">"192.168.0."</span></div><div class="line"><span class="keyword">for</span> i <span class="keyword">in</span> `seq 1 254`</div><div class="line"><span class="keyword">do</span></div><div class="line">ping -c 2 <span class="variable">$ips</span><span class="variable">$i</span> &gt;/dev/null 2&gt;/dev/null</div><div class="line"><span class="keyword">if</span> [ $? == 0 ]</div><div class="line"><span class="keyword">then</span></div><div class="line">    <span class="built_in">echo</span> <span class="string">"echo <span class="variable">$ips</span><span class="variable">$i</span> is online"</span></div><div class="line"><span class="keyword">else</span></div><div class="line">    <span class="built_in">echo</span> <span class="string">"echo <span class="variable">$ips</span><span class="variable">$i</span> is not online"</span></div><div class="line"><span class="keyword">fi</span></div><div class="line"><span class="keyword">done</span></div></pre></td></tr></table></figure>
<p>也可以使用 nmap 命令，速度比较快一点</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="function"><span class="title">myyum</span></span> () &#123;</div><div class="line"><span class="keyword">if</span> ! rpm -qa|grep -q <span class="string">"^<span class="variable">$1</span>"</span></div><div class="line"><span class="keyword">then</span></div><div class="line">    yum install -y <span class="string">"^<span class="variable">$1</span>"</span></div><div class="line"><span class="keyword">else</span></div><div class="line">    <span class="built_in">echo</span> <span class="variable">$1</span> already installed.</div><div class="line"><span class="keyword">fi</span></div><div class="line">&#125;</div><div class="line"><span class="keyword">for</span> i <span class="keyword">in</span> nmap</div><div class="line"><span class="keyword">do</span></div><div class="line">    myyum <span class="variable">$i</span></div><div class="line"><span class="keyword">done</span></div><div class="line">nmap -sP 192.168.0.0/24</div></pre></td></tr></table></figure>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/21. shell 练习-实现一个小型计算器/">shell 练习-实现一个小型计算器</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div></pre></td><td class="code"><pre><div class="line">[root@nfs01 scripts]<span class="comment"># cat bc.sh </span></div><div class="line"><span class="meta">#!/bin/sh</span></div><div class="line"><span class="comment"># First we should save the number which was inserted by user</span></div><div class="line"><span class="built_in">read</span> -p <span class="string">"Please insert the first number and the method you want to caculate:"</span> num1</div><div class="line"><span class="built_in">read</span> -p <span class="string">"Please insert the second  number and the method you want to caculate:"</span> num2</div><div class="line"><span class="built_in">read</span> -p <span class="string">"Please insert the method you want to caculate:"</span> \opt</div><div class="line"><span class="function"><span class="title">usage</span></span>()&#123;</div><div class="line">     <span class="built_in">echo</span> <span class="string">"Usage <span class="variable">$0</span> num1 num2 operation"</span></div><div class="line">     <span class="built_in">exit</span> 1</div><div class="line">&#125;</div><div class="line"> </div><div class="line"><span class="function"><span class="title">if_int</span></span>()&#123;</div><div class="line">     num=<span class="variable">$1</span></div><div class="line">     <span class="keyword">if</span> [ ! -z $(<span class="built_in">echo</span> <span class="variable">$num</span> | sed <span class="string">'s/[0-9]//g'</span>) ];<span class="keyword">then</span></div><div class="line">           usage</div><div class="line">           <span class="built_in">exit</span> 2</div><div class="line">     <span class="keyword">fi</span></div><div class="line">&#125; </div><div class="line">if_int <span class="variable">$num1</span></div><div class="line">if_int <span class="variable">$num2</span></div><div class="line"><span class="function"><span class="title">caculate</span></span>()&#123;</div><div class="line">     <span class="built_in">echo</span> <span class="string">"<span class="variable">$&#123;num1&#125;</span><span class="variable">$&#123;opt&#125;</span><span class="variable">$&#123;num2&#125;</span>=<span class="variable">$(($&#123;num1&#125;$&#123;opt&#125;$&#123;num2&#125;)</span>)"</span>  </div><div class="line">&#125;</div><div class="line"><span class="function"><span class="title">if_zero</span></span>()&#123;</div><div class="line">     <span class="keyword">if</span> [ <span class="variable">$1</span> -eq 0 ];<span class="keyword">then</span></div><div class="line">        <span class="built_in">echo</span> <span class="string">"The dividend can't be zero,please input a number which is not equal 0"</span></div><div class="line">        <span class="built_in">exit</span> 3</div><div class="line">     <span class="keyword">fi</span></div><div class="line">&#125;</div><div class="line"> </div><div class="line"><span class="keyword">case</span> <span class="variable">$opt</span> <span class="keyword">in</span></div><div class="line">     <span class="string">"+"</span>)</div><div class="line">     caculate <span class="variable">$num1</span> <span class="variable">$num2</span> <span class="variable">$opt</span></div><div class="line">     ;;</div><div class="line">     <span class="string">"-"</span>)</div><div class="line">     caculate <span class="variable">$num1</span> <span class="variable">$num2</span> <span class="variable">$opt</span></div><div class="line">     ;;</div><div class="line">     <span class="string">"*"</span>)</div><div class="line">     caculate <span class="variable">$num1</span> <span class="variable">$num2</span> <span class="variable">$opt</span></div><div class="line">     ;;</div><div class="line">     <span class="string">"/"</span>)</div><div class="line">     if_zero <span class="variable">$num2</span></div><div class="line">     result=$(<span class="built_in">echo</span> <span class="variable">$&#123;num1&#125;</span><span class="variable">$&#123;opt&#125;</span><span class="variable">$&#123;num2&#125;</span> | bc )</div><div class="line">     <span class="built_in">echo</span> <span class="variable">$result</span></div><div class="line">     ;;</div><div class="line">     <span class="string">"%"</span>)</div><div class="line">     caculate <span class="variable">$num1</span> <span class="variable">$num2</span> <span class="variable">$opt</span></div><div class="line">     ;;</div><div class="line"><span class="keyword">esac</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这个试题遇到的问题就是尽量不要进行变量的复用，变量的多次复用会导致意外的问题。比如从用户那里接受到了 <code>$opt=*</code> ，然后当把变量再赋值给另外的变量的时候那就不是 <em> 了，这个 </em> 会被解析成为当前目录下的所有文件，就会出问题，除非要求用户输入的时候指定  <code>\*</code> 这种格式，给 * 脱意。</p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/20. shell 练习-乘法口诀表/">shell 练习-乘法口诀表</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>需要两层循环</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"></div><div class="line"><span class="keyword">for</span> i <span class="keyword">in</span> `seq 1 9`</div><div class="line"><span class="keyword">do</span></div><div class="line">    <span class="keyword">for</span> j <span class="keyword">in</span> `seq 1 <span class="variable">$i</span>`</div><div class="line">    <span class="keyword">do</span></div><div class="line">        <span class="built_in">echo</span> -n <span class="variable">$i</span>*<span class="variable">$j</span>=$((i*j))</div><div class="line">        <span class="built_in">echo</span> -ne <span class="string">'\t'</span></div><div class="line">    <span class="keyword">done</span></div><div class="line"><span class="built_in">echo</span></div><div class="line"><span class="keyword">done</span></div></pre></td></tr></table></figure>
<p>输出结果</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># sh 1.sh </span></div><div class="line">1*1=1</div><div class="line">2*1=2   2*2=4</div><div class="line">3*1=3   3*2=6   3*3=9</div><div class="line">4*1=4   4*2=8   4*3=12  4*4=16</div><div class="line">5*1=5   5*2=10  5*3=15  5*4=20  5*5=25</div><div class="line">6*1=6   6*2=12  6*3=18  6*4=24  6*5=30  6*6=36</div><div class="line">7*1=7   7*2=14  7*3=21  7*4=28  7*5=35  7*6=42  7*7=49</div><div class="line">8*1=8   8*2=16  8*3=24  8*4=32  8*5=40  8*6=48  8*7=56  8*8=64</div><div class="line">9*1=9   9*2=18  9*3=27  9*4=36  9*5=45  9*6=54  9*7=63  9*8=79*9=81</div></pre></td></tr></table></figure>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/2. shell 练习-统计IP访问量/">shell 练习-统计IP访问量</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>抓包 ip 访问日志 /var/log/1.log 然后进行系统分析：要求统计出每个 IP 的访问量有多少？提示，先 awk 过滤出 ip ，然后进行排序，统计重复数</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">awk <span class="string">'&#123;print $1&#125;'</span> /var/<span class="built_in">log</span>/1.log |sort -n|uniq -c|sort-rn</div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/19. shell 练习-找规律打印数字/">shell 练习-找规律打印数字</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>请详细查看如下几个数字的规律，并使用shell脚本输出后面的十个数字。</p>
<p>10 31 53 77  105 141 …….</p>
<p>试题解析：</p>
<p>我想大多数人都会去比较这些数字的差值：</p>
<p>10&#160;&#160;31&#160;&#160;53&#160;&#160;77&#160;&#160;105&#160;&#160;141<br>&#160;&#160;21&#160;&#160;&#160;22&#160;&#160;&#160;24&#160;&#160;&#160;28&#160;&#160;&#160;36</p>
<p>但是这个差值看，并没有什么规律，而我们再仔细看的时候，发现这个差值的差值是有规律的：</p>
<p>10&#160;&#160;31&#160;&#160;53&#160;&#160;77&#160;&#160;105&#160;&#160;141<br>&#160;&#160;&#160;21&#160;&#160;&#160;22&#160;&#160;&#160;24&#160;&#160;&#160;28&#160;&#160;&#160;36<br>&#160;&#160;&#160;&#160;&#160;&#160;1&#160;&#160;&#160;&#160;2&#160;&#160;&#160;&#160;4&#160;&#160;&#160;&#160;8</p>
<p>所以，最终得出规律</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#! /bin/bash</span></div><div class="line">x=21</div><div class="line">m=10</div><div class="line"><span class="built_in">echo</span> <span class="variable">$m</span></div><div class="line"><span class="keyword">for</span> i <span class="keyword">in</span> `seq 0 14`; <span class="keyword">do</span></div><div class="line">    j=$[2**<span class="variable">$i</span>]</div><div class="line">    m=$[<span class="variable">$m</span>+<span class="variable">$x</span>]</div><div class="line">    <span class="built_in">echo</span> <span class="variable">$m</span></div><div class="line">    x=$[<span class="variable">$x</span>+<span class="variable">$j</span>]</div><div class="line"><span class="keyword">done</span></div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/18. shell 练习-解决某个分区快满的问题/">shell 练习-解决某个分区快满的问题</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>有一天，你发现你所管理的服务器磁盘某个分区马上要满了，那你如何做呢？</p>
<p>提示：</p>
<ol>
<li><p>查看和统计各个分区使用情况，把占用空间大的目录以及文件找出来，看看是不是有一些老的没有用的文件占用了空间，比如有日志，那你可以考虑写一个任务计划每天定点删除一个月之前的；</p>
</li>
<li><p>如果不能删除，那么就要想办法添加磁盘，假如你已经成功添加磁盘，请问，你如何分区，然后 如何解决某个分区快满的问题？</p>
</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="comment">## this is fdisk free total</span></div><div class="line"></div><div class="line">a=`df -h`;<span class="built_in">echo</span> -e <span class="string">"`date +%F-%T` \n<span class="variable">$a</span>"</span> &gt; fs_du.log</div><div class="line">b=`sed s/%//g fs_du.log|awk <span class="string">'$5&gt;90 &#123;print $6&#125;'</span>|sed s/Mounted//g<span class="string">'</span></div><div class="line"><span class="string">if [ -n "$b" ]</span></div><div class="line"><span class="string">then</span></div><div class="line"><span class="string">    cd $b</span></div><div class="line"><span class="string">    find . -size +100M &gt; bigsize.file</span></div><div class="line"><span class="string">    mail -s "host Pattion Use 90%" 123@123.com</span></div><div class="line"><span class="string">else</span></div><div class="line"><span class="string">    exit</span></div><div class="line"><span class="string">fi</span></div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/17. shell 练习-封ip/">shell 练习-封ip</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>需求： 根据web服务器上的访问日志，把一些请求量非常高的ip给拒绝掉！ </p>
<p>分析： 我们要做的，不仅是要找到哪些ip请求量不合法，并且还要每隔一段时间把之前封掉的ip（若不再继续请求了）给解封。 所以该脚本的关键点在于定一个合适的时间段和阈值。 </p>
<p>比如， 我们可以每一分钟去查看一下日志，把上一分钟的日志给过滤出来分析，并且只要请求的ip数量超过100次那么就直接封掉。 而解封的时间又规定为每半小时分析一次，把几乎没有请求量的ip给解封！</p>
<p>参考日志文件片段：</p>
<blockquote>
<p>157.55.39.107 [20/Mar/2015:00:01:24 +0800] www.aminglinux.com “/bbs/thread-5622-3-1.html” 200 “-“ “Mozilla/5.0 (compatible; bingbot/2.0; +<a href="http://www.bing.com/bingbot.htm" target="_blank" rel="external">http://www.bing.com/bingbot.htm</a>)”<br>61.240.150.37 [20/Mar/2015:00:01:34 +0800] www.aminglinux.com “/bbs/search.php?mod=forum&amp;srchtxt=LNMP&amp;formhash=8f0c7da9&amp;searchsubmit=true&amp;source=hotsearch” 200 “-“ “Mozilla/5.0 (compatible; bingbot/2.0; +<a href="http://www.bing.com/bingbot.htm" target="_blank" rel="external">http://www.bing.com/bingbot.htm</a>)”</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#! /bin/bash</span></div><div class="line">logfile=/home/logs/access.log</div><div class="line">d1=`date -d <span class="string">"-1 minute"</span> +%H:%M`</div><div class="line">d2=`date +%M`</div><div class="line">ipt=/sbin/iptables</div><div class="line">ips=/tmp/ips.txt</div><div class="line"></div><div class="line"><span class="function"><span class="title">block</span></span>()&#123;</div><div class="line">    grep <span class="string">"<span class="variable">$d1</span>:"</span> <span class="variable">$logfile</span>|awk <span class="string">'&#123;print $1&#125;'</span> |sort -n |uniq -c |sort -n &gt;<span class="variable">$ips</span></div><div class="line">    <span class="keyword">for</span> ip <span class="keyword">in</span> `awk <span class="string">'$1&gt;100 &#123;print $2&#125;'</span> <span class="variable">$ips</span>`; <span class="keyword">do</span></div><div class="line">        <span class="variable">$ipt</span> -I INPUT -p tcp --dport 80 -s <span class="variable">$ip</span> -j REJECT</div><div class="line">        <span class="built_in">echo</span> <span class="string">"`date +%F-%T` <span class="variable">$ip</span>"</span> &gt;&gt; /tmp/badip.txt</div><div class="line">    <span class="keyword">done</span></div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="title">unblock</span></span>()&#123;</div><div class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> `<span class="variable">$ipt</span> -nvL --line-numbers |grep <span class="string">'0.0.0.0/0'</span>|awk <span class="string">'$2&lt;15 &#123;print $1&#125;'</span>|sort -nr`; <span class="keyword">do</span></div><div class="line">        <span class="variable">$ipt</span> -D INPUT <span class="variable">$i</span></div><div class="line">    <span class="keyword">done</span></div><div class="line">    <span class="variable">$ipt</span> -Z</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="keyword">if</span> [ <span class="variable">$d2</span> == <span class="string">"00"</span> ] || [ <span class="variable">$d2</span> == <span class="string">"30"</span> ]; <span class="keyword">then</span></div><div class="line">    unblock</div><div class="line">    block</div><div class="line"><span class="keyword">else</span></div><div class="line">    block</div><div class="line"><span class="keyword">fi</span></div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/16. shell 练习-监控httpd进程/">shell 练习-监控httpd进程</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>在服务器上，写一个监控脚本。</p>
<ol>
<li><p>每隔10s去检测一次服务器上的httpd进程数，如果大于等于500的时候，就需要自动重启一下apache服务，并检测启动是否成功？</p>
</li>
<li><p>若没有正常启动还需再一次启动，最大不成功数超过5次则需要理解发邮件通知管理员，并且以后不需要再检测！ </p>
</li>
<li><p>如果启动成功后，1分钟后再次检测httpd进程数，若正常则重复之前操作（每隔10s检测一次），若还是大于等于500，那放弃重启并需要发邮件给管理员，然后自动退出该脚本。假设其中发邮件脚本为之前咱们使用的mail.py</p>
</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">check_service()</div><div class="line">&#123;</div><div class="line">    n=0</div><div class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> `seq 1 5`</div><div class="line">    <span class="keyword">do</span></div><div class="line">        /usr/<span class="built_in">local</span>/apache2/bin/apachectl restart 2&gt;/tmp/apache.err</div><div class="line">        <span class="keyword">if</span> [ $? -ne 0 ]</div><div class="line">        <span class="keyword">then</span></div><div class="line">            n=$[<span class="variable">$n</span>+1]</div><div class="line">        <span class="keyword">else</span></div><div class="line">            <span class="built_in">break</span></div><div class="line">        <span class="keyword">fi</span></div><div class="line">    <span class="keyword">done</span></div><div class="line">    <span class="keyword">if</span> [ <span class="variable">$n</span> -eq 5 ]</div><div class="line">    <span class="keyword">then</span></div><div class="line">        <span class="comment">##下面的mail.py参考https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D22Z/mail.py</span></div><div class="line">        python mai.py <span class="string">"123@qq.com"</span> <span class="string">"httpd service down"</span> `cat /tmp/apache.err`</div><div class="line">        <span class="built_in">exit</span></div><div class="line">    <span class="keyword">fi</span>  </div><div class="line">&#125;</div><div class="line">    </div><div class="line"><span class="keyword">while</span> :</div><div class="line"><span class="keyword">do</span></div><div class="line">    t_n=`ps -C httpd --no-heading |wc -l`</div><div class="line">    <span class="keyword">if</span> [ <span class="variable">$t_n</span> -ge 500 ]</div><div class="line">    <span class="keyword">then</span></div><div class="line">        /usr/<span class="built_in">local</span>/apache2/bin/apachectl restart </div><div class="line">        <span class="keyword">if</span> [ $? -ne 0 ]</div><div class="line">        <span class="keyword">then</span></div><div class="line">            check_service</div><div class="line">        <span class="keyword">fi</span></div><div class="line">        sleep 60</div><div class="line">    <span class="keyword">fi</span></div><div class="line">    sleep 10</div><div class="line"><span class="keyword">done</span></div></pre></td></tr></table></figure>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/15. shell 练习-创建用户并设置密码/">shell 练习-创建用户并设置密码</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>用shell脚本实现如下需求：<br>添加user_00 - user_09 10个用户，并且给他们设置一个随机密码，密码要求10位包含大小写字母以及数字，注意需要把每个用户的密码记录到一个日志文件里。</p>
<p>提示： </p>
<ol>
<li>随机密码使用命令 mkpasswd</li>
<li>在脚本中给用户设置密码，可以使用echo 然后管道passwd命令</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="keyword">for</span> i <span class="keyword">in</span> `seq -w 00 09`</div><div class="line"><span class="keyword">do</span></div><div class="line">    useradd user_<span class="variable">$i</span></div><div class="line">    p=`mkpasswd -s 0 -l 10`</div><div class="line">    <span class="built_in">echo</span> <span class="string">"user_<span class="variable">$i</span> <span class="variable">$p</span>"</span> &gt;&gt;/tmp/user0_9.pass</div><div class="line">    <span class="built_in">echo</span> <span class="variable">$p</span> |passwd --stdin user_<span class="variable">$i</span></div><div class="line"><span class="keyword">done</span></div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/14. shell 练习-输入数字执行对应命令/">shell 练习-输入数字执行对应命令</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>写一个脚本实现如下功能：  输入一个数字，然后运行对应的一个命令。显示命令如下：*cmd meau**  1—date 2–ls 3–who 4–pwd</p>
<p>当输入1时，会运行date, 输入2时运行ls, 依此类推。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="built_in">echo</span> <span class="string">"*cmd meau**  1--date  2--ls  3--who  4--pwd"</span></div><div class="line"><span class="built_in">read</span> -p <span class="string">"please input a number 1-4:"</span> n</div><div class="line"><span class="keyword">case</span> <span class="variable">$n</span> <span class="keyword">in</span></div><div class="line">    1)</div><div class="line">        date</div><div class="line">        ;;</div><div class="line">    2)</div><div class="line">        ls</div><div class="line">        ;;</div><div class="line">    3)</div><div class="line">        who</div><div class="line">        ;;</div><div class="line">    4)</div><div class="line">        <span class="built_in">pwd</span></div><div class="line">        ;;</div><div class="line">    *)</div><div class="line">        <span class="built_in">echo</span> <span class="string">"please input a number: 1-4"</span></div><div class="line">        ;;</div><div class="line"><span class="keyword">esac</span></div></pre></td></tr></table></figure>

	

	

</article>





	<span class="different-posts">📖 <a href="/page/13">more posts</a> 📖</span>



	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
